// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        //爬行速度
        climbSpeed : 0,
        //下落速度
        dropSpeed:0,
        //最高位置
        maxPosY:0,
        //最低位置
        minPosY:0
    },

    Init:function(game){
        this.game=game;
        this.currentRoad = 3;
        this.changeRoad(this.currentRoad);
    },
    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        this.climbing = false;
        this.droping = false;

        
        // 初始化键盘输入监听
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    },

    onDestroy () {
        // 取消键盘输入监听
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
        
    },

    onKeyDown(event){
        switch(event.keyCode){
            case cc.macro.KEY.w:
                this.climbing = true;
                this.droping = false;
                break;
            case cc.macro.KEY.s:
                this.climbing=false;
                this.droping = true;
                break;
            case cc.macro.KEY.d:
                this.goRightRoad();
                break;
            case cc.macro.KEY.a:
                this.goLeftRoad();
                break;
        }
    },

    onKeyUp(event){
        switch(event.keyCode){
            case cc.macro.KEY.w:
                this.climbing = false;
                break;
            case cc.macro.KEY.s:
                this.droping =false;
                break;
        }
    },

    goRightRoad(){
        if(this.currentRoad<5){
            this.currentRoad +=1;
            this.changeRoad(this.currentRoad);
        }
    },

    goLeftRoad(){
        if(this.currentRoad>0){
            this.currentRoad-=1;
            this.changeRoad(this.currentRoad);
        }
    },


    start () {

    },

    update (dt) {
        //爬竹子
        if(this.climbing){
            this.node.y+=this.climbSpeed*dt;       
        }
        else if(this.droping){
            this.node.y-=this.dropSpeed*dt;
        }

        //最高位置
        if(this.node.y>this.maxPosY){
            this.node.y=this.maxPosY;
        }
        else if(this.node.y<this.minPosY){
            this.node.y=this.minPosY;
        }
    },

    startMoveAt:function(road){
        this.node.setPosition(this.game.roadDic[road]);
        this.currentRoad=road;
    },

    changeRoad:function(road){
        this.node.setPosition(this.game.roadDic[road],this.node.y);
        this.currentRoad=road;
    },

    gameOver:function(){

    },
});
